package com.ali.common;

import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class HttpClientUtil {
    /**
     * Http的Get请求
     */
    public static Map<String, Object> get(String url, Map<String, String> headers) {
        Map<String, Object> response;

        try (CloseableHttpClient closeableHttpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(url);

            //设置请求头
            if (headers != null) {
                for (String key : headers.keySet()) {
                    httpGet.setHeader(key, headers.get(key));
                }
            }

            response = getResponse(closeableHttpClient, httpGet);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return response;
    }

    /**
     * 获取返回信息
     */
    private static Map<String, Object> getResponse(CloseableHttpClient closeableHttpClient, HttpRequestBase httpRequestBase) {
        Map<String, Object> result = new HashMap<>();

        try (CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpRequestBase)) {
            //获取响应码
            int responseCode = closeableHttpResponse.getStatusLine().getStatusCode();
            result.put("responseCode", responseCode);

            //获取返回参数
            String response = EntityUtils.toString(closeableHttpResponse.getEntity(), StandardCharsets.UTF_8);
            result.put("response", response);

            //获取返回头
            Header[] responseHeaders = closeableHttpResponse.getAllHeaders();
            for (Header responseHeader : responseHeaders) {
                result.put(responseHeader.getName(), responseHeader.getValue());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return result;
    }

    /**
     * Http的Post请求
     */
    public static Map<String, Object> post(String url, String content, Map<String, String> headers) {
        Map<String, Object> response;

        try (CloseableHttpClient closeableHttpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);

            //设置传输参数格式
            httpPost.setHeader("Content-Type", "application/json");

            //设置传输参数
            httpPost.setEntity(new StringEntity(content, "UTF-8"));

            //设置请求头
            if (headers != null) {
                for (String key : headers.keySet()) {
                    httpPost.setHeader(key, headers.get(key));
                }
            }

            response = getResponse(closeableHttpClient, httpPost);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return response;
    }
}